from django.shortcuts import render

from django.http import JsonResponse
from rest_framework.views import APIView
from rest_framework.response import Response
from chat_bot import search



class chat_bot(APIView):
    def post(self, request, *args, **kwargs):
        search_obj = search.Search()
        ret = {'ret':None, 'data': None, 'msg':None}
        try:
            sentence = request.data.get('sentence')
            answer = search_obj.ChatBot(sentence)
            if (answer):
                ret['ret'] = 0
                ret['data'] = {'session': 10000, 'answer':answer}
                ret['msg'] = "ok"  
            else:
                ret['msg'] = "fail"
        except:
            ret['ret'] = 201
            ret['msg'] = "fail"
        return Response(ret)
        # try:
        #     username = request.data.get('username')
        #     password = request.data.get('password')
        #     obj = User.objects.filter(username=username, password=password).first()
        #     if not obj:
        #         ret['code'] = 1001
        #         ret['mes'] = '用户名或密码错误！'
        #         return Response(ret)
        #     ret['mes'] = '登陆成功'
        # except Exception as e:
        #     ret['code'] = 1002
        #     ret['mes'] = '请求异常'
        # return Response(ret)
# Create your views here.

# Create your views here.
